<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/3/2
 * Time: 10:43
 */

$http = new swoole_http_server('0.0.0.0', 9501);


$http->set(array(

    'worker_num' => 4,    // 启动的worker进程  CPU的1-4倍最合理
//    'daemonize' => 1,    // 程序将转入后台作为守护进程运行


));


$http->on('request', function (swoole_http_request $request, swoole_http_response $response) {

    print_r($request);

//    $response->status(404);
//    $response->end('404 not found');

    // 静态的文件请求
    $pathInfo = $request->server['path_info'];

    $filename = __DIR__ . $pathInfo;
    if (is_file($filename)) {

        $ext = pathinfo($pathInfo, PATHINFO_EXTENSION);
        if ($ext == 'php') {    // 动态请求

            ob_start();
            include_once $filename;
            $content = ob_get_contents();
            ob_end_clean();
            $response->end($content);
        } else {
            $contentTypes = include('contextType.php');
            $response->header('Content-Type', $contentTypes[$ext]);
            $content = file_get_contents($filename);
            $response->end($content);
        }
    } else {
        $response->status(404);
        $response->end('not file');
    }

});

$http->start();